<?php include "../../layout/header.php"; ?>
<?php include "../../layout/navleft.php"; ?>
<?php include "../../db/dbconnect.php"; ?>
<link rel="stylesheet" href="<?php echo($siteroot);?>css/admin.css" type="text/css" />

<div id="content_small">
	<div class="container_brown">
		<font face="Verdana" color=brown size=2.5px><a href="admin.php" >Admin</a></font>
        <font face="Verdana" color=brown size=2.5px><a href="admin_find_user.php"> > Find User</a></font>
        <font face="Verdana" color=brown size=2.5px> > Edit User</font><br><br>
	
		<div class="admin_div">
    <form action="admin_monitor_user.php" method="post">
        <?php
        if(isset($_POST['submit_button'])) $mode = $_POST['submit_button']; // test
        else $mode = ""; // test
        //if(isset($_POST['submit_button']) && ($_POST['submit_button'] == "Edit") || ($_POST['submit_button'] == "Submit")) {
        if(!is_null($mode)){
	        $updateItem = $_POST['uid'];
	        if(($mode == "Submit")) {
	            $query;
	            $updateStatement = "UPDATE User SET ";
	            $newUserName = isset($_POST['username']) ? trim($_POST['username']) : "";
	            $newFirst_name = isset($_POST['first_name']) ? trim($_POST['first_name']) : "";
	            $newLast_name = isset($_POST['last_name']) ? trim($_POST['last_name']) : "";
	            $newEmail = isset($_POST['email']) ? trim($_POST['email']) : "";
	            $newBday = isset($_POST['bday']) ? trim($_POST['bday']) : "";
	            $newGender = isset($_POST['gender']) ? trim($_POST['gender']) : "";
	            $newLevel = isset($_POST['level']) ? trim($_POST['level']) : "";
	            $newExp = isset($_POST['exp']) ? trim($_POST['exp']) : "";
	            $newGold = isset($_POST['gold']) ? trim($_POST['gold']) : "";
	            $newFlid = isset($_POST['flid']) ? trim($_POST['flid']) : "";
	            $newRole = isset($_POST['role']) ? trim($_POST['role']) : "";
	            $newIsBanned = isset($_POST['isBanned']) ? trim($_POST['isBanned']) : "";
	            $newInventory = isset($_POST['inventory']) ? trim($_POST['inventory']) : "";
	            $newCid = isset($_POST['cid']) ? trim($_POST['cid']) : "";
	            $newDeaths = isset($_POST['deaths']) ? trim($_POST['deaths']) : "";
	            $newBugkills = isset($_POST['bugkills']) ? trim($_POST['bugkills']) : "";
	            $newQCreated = isset($_POST['qcreated']) ? trim($_POST['qcreated']) : "";
	            $newPic = isset($_POST['pic']) ? trim($_POST['pic']) : "";
	
	            $validUpdate = 0;
	            if (!empty($newUserName)) {
	                $validUpdate = 1;
	                $updateStatement .= "username = \"".$newUserName."\" ";
	            }
	            if (!empty($newFirst_name)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "first_name = \"".$newFirst_name."\" ";
	            }
	            if (!empty($newLast_name)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "last_name = \"".$newLast_name."\" ";
	            }
	            if (!empty($newEmail)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "email = \"".$newEmail."\" ";
	            }
	            if (!empty($newBday)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "bday = \"".$newBday."\" ";
	            }
	            if (!empty($newGender)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "gender = \"".$newGender."\" ";
	            }
	            if (!empty($newLevel)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "level = \"".$newLevel."\" ";
	            }
	            if (!empty($newExp)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "exp = \"".$newExp."\" ";
	            }
	            if (!empty($newGold)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "gold = \"".$newGold."\" ";
	            }
	            if (!empty($newFlid)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "flid = \"".$newFlid."\" ";
	            }
	            if (!empty($newRole)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "role = \"".$newRole."\" ";
	            }
	            if (!empty($newIsBanned)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "isBanned = \"".$newIsBanned."\" ";
	            }
	            if (!empty($newInventory)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "inventory = \"".$newInventory."\" ";
	            }
	            if (!empty($newCid)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "cid = \"".$newCid."\" ";
	            }
	            if (!empty($newDeaths)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "deaths = \"".$newDeaths."\" ";
	            }if (!empty($newBugkills)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "bugkills = \"".$newBugkills."\" ";
	            }if (!empty($newQcreated)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "qcreated = \"".$newQcreated."\" ";
	            }if (!empty($newPic)) {
	                if ($validUpdate > 0) {
	                    $updateStatement .= ", ";
	                }
	                $validUpdate = 1;
	                $updateStatement .= "pic = \"".$newPic."\" ";
	            }$updateStatement .= "WHERE uid=".$updateItem;
	            if ($validUpdate > 0) {
	                $result = mysql_query($updateStatement);
	                if($result){?>
	                	<span id="edit_user_result">User information updated.</span><BR>
	                <?php
	                }
	            }
        	}
            echo "<table id=\"edit_user_table\">";
            $query = "SELECT * from User WHERE uid = ".$updateItem;
            $rs = mysql_query($query);
            while ($row = mysql_fetch_array($rs)) {?>
                <tr><td class="attribute">Username</td><td><input type="text" value="<?php echo($row['username']);?>" name="userName"></td></tr>
                <tr><td class="attribute">First Name</td><td><input type="text" value="<?php echo($row['first_name']);?>" name="first_name"></td></tr>
                <tr><td class="attribute">Last Name</td><td><input type="text" value="<?php echo($row['last_name']);?>" name="last_Name"></td></tr>
                <tr><td class="attribute">Email</td><td><input type="text" value="<?php echo($row['email']);?>" name="email"></td></tr>
                <tr><td class="attribute">Bday</td><td><input type="text" value="<?php echo($row['bday']);?>" name="bday"></td></tr>
                <tr><td class="attribute">Gender</td><td>
                	<select class="select_box" selected="<?php echo($row['gender']);?>" name="gender">
                		<option value="1">Male</option>
                		<option value="2">Female</option>
                	</select></td></tr>
                <tr><td class="attribute">Level</td><td><input type="text" value="<?php echo($row['level']);?>" name="level"></td></tr>
                <tr><td class="attribute">Exp</td><td><input type="text" value="<?php echo($row['exp']);?>" name="exp"></td></tr>
                <tr><td class="attribute">Gold</td><td><input type="text" value="<?php echo($row['gold']);?>" name="gold"></td></tr>
                <tr><td class="attribute">Flid</td><td><input type="text" value="<?php echo($row['flid']);?>" name="flid"></td></tr>
                <tr><td class="attribute">Role</td><td>
                	<select class="select_box" selected="<?php echo($row['role']);?>" name="role">
                		<option value="A">A</option>
                		<option value="V">V</option>
                	</select></td></tr>
                <tr><td class="attribute">Banned?</td><td>
                	<select class="select_box" selected="<?php echo($row['isBanned']);?>" name="isBanned">
                		<option value="0">No</option>
                		<option value="1">Yes</option>
                	</select></td></tr>
                <tr><td class="attribute">Inventory</td><td><input type="text" value="<?php echo($row['inventory']);?>" name="inventory"></td></tr>
                <tr><td class="attribute">Cid</td><td><input type="text" value="<?php echo($row['cid']);?>" name="cid"></td></tr>
                <tr><td class="attribute">Deaths</td><td><input type="text" value="<?php echo($row['deaths']);?>" name="deaths"></td></tr>
                <tr><td class="attribute"># Bug kills</td><td><input type="text" value="<?php echo($row['bugkills']);?>" name="bugkills"></td></tr>
                <tr><td class="attribute">Qcreated</td><td><input type="text" value="<?php echo($row['qcreated']);?>" name="qcreated"></td></tr>
                <input type="hidden" name="uid" value="<?php echo($row['uid']);?>"/>
			<?php
            }
            echo "</table>";
            echo "<hr style:'width:350px'>";
        }


        ?>
        <table style="width:100%">
            <tr>
                <td style="text-align: right">
                    <input type="submit" name ="submit_button" value="Submit"/>
                </td>
            </tr>
        </table>
    </form>
    </div>
	</div>
</div>
<?php include "../../layout/navright.php"; ?>
<?php include "../../layout/footer.php"; ?>